package com.example.controller;

import com.example.common.Result;
import com.example.entity.Comment;
import com.example.entity.Film;
import com.example.mapper.CommentMapper;
import com.example.service.FilmService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/film")
public class FilmController {

    @Autowired
    private FilmService filmService;

    @Autowired
    private CommentMapper commentMapper;
    /**
     * 查询所有分类
     * @return 分类列表
     */
    @GetMapping("/selectAll")
    public Result selectAll(Film film) {
        List<Film> films = filmService.selectAll(film);
        return Result.success(films);
    }

    /**
     * 查询top5的电影列表
     * @param film
     * @return
     */
    @GetMapping("/selectTop5")
    public Result selectTop5(Film film) {
        List<Film> films = filmService.selectTop5(film);
        return Result.success(films);
    }

    /**
     * 根据id查询电影
     * @param id
     * @return film
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable("id") Integer id) {
        Film film = filmService.selectById(id);
        return Result.success(film);
    }

    @GetMapping("/selectRecommend/{id}")
    public Result selectRecommend(@PathVariable("id") Integer id) {
        List<Film> films = filmService.selectRecommend(id);
        return Result.success(films);
    }

    /**
     * 分页查询分类
     * @param pageNum
     * @param pageSize
     * @return 分页结果
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(Film film, @RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Film> FilmPageInfo = filmService.selectByPage(film, pageNum, pageSize);
        return Result.success(FilmPageInfo);
    }

    /**
     * 新增分类
     * @param film
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Film film) {
        filmService.add(film);
        return Result.success();
    }

    /**
     * 修改分类
     * @param film
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Film film) {
        filmService.update(film);
        return Result.success();
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        filmService.delete(id);
        return Result.success();
    }

    /**
     * 获取电影评分数据
     */
    @GetMapping("/getMovieRatingBarData")
    public Result getMovieRatingBarData() {
        List<Comment> comments = commentMapper.selectAll(null);
        Map<String, Object> map = new HashMap<>();
        // 获取所有电影名称
        Set<String> filmNameSet = comments.stream().map(Comment::getFilmName).collect(Collectors.toSet());
        map.put("filmName", filmNameSet);
        // 获取所有电影评分
        Map<String, Double> filmScoreMap = comments.stream().collect(Collectors.groupingBy(Comment::getFilmName, Collectors.averagingDouble(Comment::getScore)));
        map.put("filmScore", filmScoreMap);
        return Result.success(map);
    }

    /**
     * 获取电影的图片数据
     */
    @GetMapping("/getMovieImgData")
    public Result getMovieImgData() {
        List<Film> films = filmService.selectAll(null);
        return Result.success(films);
    }
}
